#include "UnitConversionDialog.h"
#include "ui_UnitConversionDialog.h"
#include <QMessageBox>
#include <QComboBox>

UnitConversionDialog::UnitConversionDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UnitConversionDialog),
    m_currentLengthUnit("mm"),
    m_currentTimeUnit("us"),
    m_currentWavelengthUnit("nm")
{
    ui->setupUi(this);
    
    // 设置窗口模态
    setWindowModality(Qt::ApplicationModal);
    
    // 设置默认转换比例为1.0
    ui->lengthRatioLineEdit->setText("1.0");
    ui->timeRatioLineEdit->setText("1.0");
    ui->wavelengthRatioLineEdit->setText("1.0");
    
    // 设置长度单位下拉框选项
    ui->lengthUnitComboBox->addItems({"cm", "mm"});
    ui->lengthUnitComboBox->setCurrentText("mm");
    
    // 设置时间单位下拉框选项
    ui->timeUnitComboBox->addItems({"us", "ns", "ps"});
    ui->timeUnitComboBox->setCurrentText("us");
    
    // 设置波长单位下拉框选项
    ui->wavelengthUnitComboBox->addItem("nm");
    ui->wavelengthUnitComboBox->setCurrentText("nm");
}

UnitConversionDialog::~UnitConversionDialog()
{
    delete ui;
}

void UnitConversionDialog::setLengthRatio(double ratio)
{
    ui->lengthRatioLineEdit->setText(QString::number(ratio));
}

void UnitConversionDialog::setTimeRatio(double ratio)
{
    ui->timeRatioLineEdit->setText(QString::number(ratio));
}

void UnitConversionDialog::setWavelengthRatio(double ratio)
{
    ui->wavelengthRatioLineEdit->setText(QString::number(ratio));
}

double UnitConversionDialog::getLengthRatio() const
{
    double ratio = 1.0;
    isValidNumber(ui->lengthRatioLineEdit->text(), ratio);
    return ratio;
}

double UnitConversionDialog::getTimeRatio() const
{
    double ratio = 1.0;
    isValidNumber(ui->timeRatioLineEdit->text(), ratio);
    return ratio;
}

double UnitConversionDialog::getWavelengthRatio() const
{
    double ratio = 1.0;
    isValidNumber(ui->wavelengthRatioLineEdit->text(), ratio);
    return ratio;
}

void UnitConversionDialog::on_okButton_clicked()
{
    // 验证所有转换比例输入
    double lengthRatio, timeRatio, wavelengthRatio;
    
    if (!isValidNumber(ui->lengthRatioLineEdit->text(), lengthRatio) || lengthRatio <= 0) {
        QMessageBox::warning(this, "输入错误", "长度单位转换比例必须为正数");
        ui->lengthRatioLineEdit->setFocus();
        return;
    }
    
    if (!isValidNumber(ui->timeRatioLineEdit->text(), timeRatio) || timeRatio <= 0) {
        QMessageBox::warning(this, "输入错误", "时间单位转换比例必须为正数");
        ui->timeRatioLineEdit->setFocus();
        return;
    }
    
    if (!isValidNumber(ui->wavelengthRatioLineEdit->text(), wavelengthRatio) || wavelengthRatio <= 0) {
        QMessageBox::warning(this, "输入错误", "波长单位转换比例必须为正数");
        ui->wavelengthRatioLineEdit->setFocus();
        return;
    }
    
    // 发出单位变化信号
    emit unitsChanged(
        getLengthUnit(),
        getTimeUnit(),
        getWavelengthUnit()
    );
    
    // 接受对话框
    accept();
}

void UnitConversionDialog::on_cancelButton_clicked()
{
    // 拒绝对话框
    reject();
}

bool UnitConversionDialog::isValidNumber(const QString &text, double &value) const
{
    bool ok;
    value = text.toDouble(&ok);
    return ok;
}

QString UnitConversionDialog::getLengthUnit() const
{
    if (ui->lengthUnitComboBox && ui->lengthUnitComboBox->count() > 0) {
        return ui->lengthUnitComboBox->currentText();
    }
    return "mm"; // 默认返回mm
}

QString UnitConversionDialog::getTimeUnit() const
{
    if (ui->timeUnitComboBox && ui->timeUnitComboBox->count() > 0) {
        return ui->timeUnitComboBox->currentText();
    }
    return "us"; // 默认返回us
}

QString UnitConversionDialog::getWavelengthUnit() const
{
    if (ui->wavelengthUnitComboBox && ui->wavelengthUnitComboBox->count() > 0) {
        return ui->wavelengthUnitComboBox->currentText();
    }
    return "nm"; // 默认返回nm
}

void UnitConversionDialog::setLengthUnit(const QString &unit)
{
    if (ui->lengthUnitComboBox && ui->lengthUnitComboBox->findText(unit) != -1) {
        ui->lengthUnitComboBox->setCurrentText(unit);
    }
}

void UnitConversionDialog::setTimeUnit(const QString &unit)
{
    if (ui->timeUnitComboBox && ui->timeUnitComboBox->findText(unit) != -1) {
        ui->timeUnitComboBox->setCurrentText(unit);
    }
}

void UnitConversionDialog::setWavelengthUnit(const QString &unit)
{
    if (ui->wavelengthUnitComboBox && ui->wavelengthUnitComboBox->findText(unit) != -1) {
        ui->wavelengthUnitComboBox->setCurrentText(unit);
    }
}

double UnitConversionDialog::getUnitConversionFactor(const QString &unitType, const QString &unit) const
{
    // 单位转换因子：从基本单位到所选单位的转换系数
    if (unitType == "length") {
        // 基本单位：μm
        if (unit == "cm") return 10000.0; // 1 cm = 10000 μm
        if (unit == "mm") return 1000.0;  // 1 mm = 1000 μm
    } else if (unitType == "time") {
        // 基本单位：ms
        if (unit == "us") return 1000.0;  // 1 ms = 1000 μs
        if (unit == "ns") return 1000000.0; // 1 ms = 1000000 ns
        if (unit == "ps") return 1000000000.0; // 1 ms = 1000000000 ps
    } else if (unitType == "wavelength") {
        // 基本单位：nm
        if (unit == "nm") return 1.0; // 1 nm = 1 nm
    }
    return 1.0; // 默认返回1.0
}

double UnitConversionDialog::getConversionFactorBetweenUnits(const QString &unitType, const QString &fromUnit, const QString &toUnit) const
{
    // 计算从fromUnit到toUnit的转换因子
    double fromBaseFactor = getUnitConversionFactor(unitType, fromUnit);
    double toBaseFactor = getUnitConversionFactor(unitType, toUnit);
    
    // 从fromUnit到toUnit的转换因子 = (fromUnit到基本单位的因子) / (toUnit到基本单位的因子)
    // 例如：从cm到mm = (10000 μm/cm) / (1000 μm/mm) = 10 mm/cm
    return fromBaseFactor / toBaseFactor;
}

void UnitConversionDialog::on_lengthUnitComboBox_currentIndexChanged(const QString &unit)
{
    // 获取当前输入的比例值
    double currentRatio = 1.0;
    isValidNumber(ui->lengthRatioLineEdit->text(), currentRatio);
    
    // 计算从旧单位到新单位的转换因子
    double conversionFactor = getConversionFactorBetweenUnits("length", m_currentLengthUnit, unit);
    
    // 更新比例值
    double newRatio = currentRatio / conversionFactor; // 注意这里用除法，因为我们要保持实际值不变
    ui->lengthRatioLineEdit->setText(QString::number(newRatio, 'g', 10));
    
    // 更新当前单位
    m_currentLengthUnit = unit;
}

void UnitConversionDialog::on_timeUnitComboBox_currentIndexChanged(const QString &unit)
{
    // 获取当前输入的比例值
    double currentRatio = 1.0;
    isValidNumber(ui->timeRatioLineEdit->text(), currentRatio);
    
    // 计算从旧单位到新单位的转换因子
    double conversionFactor = getConversionFactorBetweenUnits("time", m_currentTimeUnit, unit);
    
    // 更新比例值
    double newRatio = currentRatio / conversionFactor;
    ui->timeRatioLineEdit->setText(QString::number(newRatio, 'g', 10));
    
    // 更新当前单位
    m_currentTimeUnit = unit;
}

void UnitConversionDialog::on_wavelengthUnitComboBox_currentIndexChanged(const QString &unit)
{
    // 获取当前输入的比例值
    double currentRatio = 1.0;
    isValidNumber(ui->wavelengthRatioLineEdit->text(), currentRatio);
    
    // 计算从旧单位到新单位的转换因子
    double conversionFactor = getConversionFactorBetweenUnits("wavelength", m_currentWavelengthUnit, unit);
    
    // 更新比例值
    double newRatio = currentRatio / conversionFactor;
    ui->wavelengthRatioLineEdit->setText(QString::number(newRatio, 'g', 10));
    
    // 更新当前单位
    m_currentWavelengthUnit = unit;
}
